package com.blogs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blogs.component.BaseActivity;
import com.blogs.service.SendIngService;
import com.blogs.tools.DBSharedPreferences;
import com.cnblogs.R;
import com.cnblogs.R.color;

public class IngPublicActivity extends BaseActivity implements OnClickListener{
	private DBSharedPreferences sp;
	private RelativeLayout title_top_bg;
	private Button title_include_back;
	private TextView title_include_title;
	private Toast toast;
	private EditText ing_public_et;
	private Button ing_public_send_btn;
	private RelativeLayout ing_public_bg;
	private RelativeLayout ing_public_buttom_ll;
	
	private int ContentId;
	private int ReplyTo;
	private int ParentCommentId;
	private int position;
	private String AtAuthor;
	private String FromMsg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ing_public);
		sp= new DBSharedPreferences(this);
		if(sp.GetSessionID().equals(""))
		{
//			this.showToast("请先登录");
			Intent intent=new Intent();
			intent.setClass(IngPublicActivity.this, UserLoginActivity.class);
			startActivity(intent);
			finish();
		}
		ViewGroup headview = (ViewGroup) findViewById(R.id.main_title_include);
    	title_top_bg=(RelativeLayout)headview;
		title_include_title = (TextView) headview.findViewById(R.id.title_include_title);
		title_include_back = (Button) headview.findViewById(R.id.title_include_back);
		title_include_title.setText("发闪存");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(this);
		
		ing_public_bg=(RelativeLayout)findViewById(R.id.ing_public_bg);
		ing_public_buttom_ll=(RelativeLayout)findViewById(R.id.ing_public_buttom_ll);
		
		ing_public_et=(EditText)findViewById(R.id.ing_public_et);
		ing_public_send_btn=(Button)findViewById(R.id.ing_public_send_btn);
		ing_public_send_btn.setOnClickListener(this);
		
		this.position=getIntent().getIntExtra("position", -1);
		this.ContentId=getIntent().getIntExtra("ContentId", 0);
		this.ReplyTo=getIntent().getIntExtra("ReplyTo", 0);
		this.ParentCommentId=getIntent().getIntExtra("ParentCommentId", 0);
		this.AtAuthor=getIntent().getStringExtra("AtAuthor");
		this.FromMsg=getIntent().getStringExtra("FromMsg")==null?"":getIntent().getStringExtra("FromMsg");
		if(!FromMsg.equals(""))
		{
			ing_public_et.setHint("回复:@"+this.AtAuthor+" [  "+Html.fromHtml( this.FromMsg)+"  ]");
		}
//		if(this.ParentCommentId!=0)
//		{
//			ing_public_et.setText("@"+this.AtAuthor+"：");
//			ing_public_et.setSelection(ing_public_et.length());
//		}
		this.InitSkin();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			ing_public_bg.getBackground().setLevel(2);
			ing_public_buttom_ll.getBackground().setLevel(2);
			ing_public_et.setTextColor(getResources().getColor(color.title_text_night));
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			ing_public_bg.getBackground().setLevel(1);
			ing_public_buttom_ll.getBackground().setLevel(1);
			ing_public_et.setTextColor(getResources().getColor(color.title_text_light));
		}
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==ing_public_send_btn)
		{
			if(ing_public_et.getEditableText().toString().trim().length()==0)
			{
				showToast("亲,你好像没有写东西哦!");
				return;
			}
			String con=ing_public_et.getEditableText().toString();
			if(this.ParentCommentId!=0)
			{
				con="@"+this.AtAuthor+"："+con;
			}
			Intent intent=new Intent(this,SendIngService.class);
			intent.putExtra("sid", sp.GetSessionID());
			intent.putExtra("position", position);
			intent.putExtra("ContentId", ContentId);
			intent.putExtra("niceNmae", sp.GetNickName());
			intent.putExtra("ContentId", ContentId);
			intent.putExtra("ReplyTo", ReplyTo);
			intent.putExtra("ParentCommentId", ParentCommentId);//是否添加来源
			intent.putExtra("Content", con);
			intent.putExtra("avatar",sp.GetUserAvatar());
			intent.putExtra("author",sp.GetNickName());
	        startService(intent);
			this.showToast("评论已进入发送队列");
			this.finish();
		}
		else if(v==title_include_back)
		{
			finish();
		}
	}
	private void showToast(String text)
	{
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
}
